package com.kgc.untils;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.kgc.entity.UserPayment;

import java.util.UUID;

public class AlipayConfig {

    // 作为身份标识的应用ID
    private static String app_id = "9021000122690432";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCJqbEXQY8WQEdqGPskdgaKRVZH+LdrUDEnf3EVlePk9U4CwOygYXW7wvKE633ewwHmORkxEo3PhFUlyUm5TxYkqN4H84JC5Bo7IrRdT1T5Lwcno7j2jIJhAm7sxrfRfM5L6dOmAGJvJMgNwpeF64oCWSFEji5UKmU/xiz5k2bEMw+FFuH9hH6FdLEp1zD7Pr7+V1SYDdQ/VqSw1am17ARObWK06JxWVBeN31Dr9n+/GduDBwZvGTtkxCPxH3Bs8EtD+xkHwk/Jzzsj7LqVbX+IqklvvLoz3p+U6uxMWxdioZSx18U22dQTsJvVIRWgMvXe44NgmhCWJk8bbrsoygmLAgMBAAECggEAJdwtgB/folb7TQqhx11OXyjCmfilOTZMzngWOcGHc6ijU86LRXCIWKM836GEdIddNlvtublIlxHl+QZq6DP/+wUWS0mFQbBKJolc4Wy4CfANH7G/ZM8v7E4nA6eDVnHkqqwcMUNZzwdmnaIu2f31br/kjl4sD6m5PtjZ9am8UajxuGcORQx4FMMEP9mGjfFYn1So+1maQFjV5Sbj6MnGZyvW/K9d3HkElUt+yc8FmYpvCs7rbZFrW1jZ5+teibex23ksRsr560vWvXE/q+IytfzfN+qNe5gpgdr/2Lo3rrFa2l8vDSURYiWkkS8sQ747cEwWN4Uzz4ECughs1721gQKBgQDLxaFb5CuFzazqvM7hzrbF7lAHX1NZtkmoaph92KFMRPlnU004Pz7/z1UwwgqO3I8iuD/7a6liZEA1xqwTNQ4HhQ/eC6tM6OpdyDNxBUofa4lte58YGovfH2VFvYQPYd6atx/nZ9C1/yGz3nh4/Zpzqcn4yvoCqZocBX6NjGXsOQKBgQCs8lpFum5Cq848yKWXOHabxA0XlwBkk+o9o1gzBwaN+1Tf4FLcer+he7nngKZL5D7swiu9rw6vHq4f7FX082Rt+EHRMvtj04SYJEY5lRx21h4r/fngu7qIo9FNMd1GXvFte/757TOid1Az4ezv/tMmwlgOkSUtVObXda4tYZwr4wKBgGZ04EUL3LZSS/dbJ8px3YR35tVOIOKivTZpInNkUPXAWIaTY+g8JLkKzpLNffGHBJE4gzMvVuOZ9I8Vbb/alUtCpE/JHkB1fCHbX8IcHsDdmSPrXHsKHfn6FzmRqKzzULIKEYiQsmhP9+owmfJ70QUHAf6OUFa2YD5mpNMXHMhBAoGACpRUs1lBL3DlD9DSWFrK3qJyjpPdIUnvFVAtsAlo/Dx4p61IRFZ57H3uFFxJT/EpbEBAfrz7r/i3fBpv86kD5ro+BHcS67tfrBLj/NfDQv28IMLLi1TQh1VGE2vOdmSObj0cjttUwkZ8MIBMhy3WatOOoW19kbVU3a9ENWB4NjMCgYBu9KMZ05licfMGEyW11IcjoQpWT/1n8Ez6eN3uJqe08hViz+HLmiGExPXg7MzgEIjFpndntIUaCOhIm2tK5HjzG/BQV3ovul3HgCt0IyOD5aJ/nPLCkHKMA8G9FyDRlbU4E/9S5Zk+XaVQiGzxUpxe5YUIptuXaD0q2gnKxTuFFw==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA32J7K+RPinxfBQtOANFMPDT7xtjoryHqSeFNHs4PA0ZcX817BZLtuGgck+LUHS0gjdoFn5peazZRD/87tou9Ep5P1HOWQrccUZOOPGw98W9HLbMJA1KmuXpNjg7ghZExWIjSjrK+gDnYEV+cpwBxlK/z0FFWF7IgptGHz9hKtHIWHbPiVKYPEMbyX1oMp8txbaigVjaY+/0bHvCyxRVHnXOACtyfaJhb2QCZOBwsfIGLzfI3zN3bjm/Gy6eLdSyZMW0RZmdClBFNvpiMvLd4RmLyvVJMIlCtgfhSGa+oR1w6arWfWzCrCAYgvznYC4idJmcKdG/i8mii/ZEArr7TIQIDAQAB";

    // 服务器异步通知页面路径
    private static String notify_url = "http://localhost:8080/PayServlet?opt=status";

    // 页面跳转同步通知页面路径
    private static String return_url = "http://localhost:8080/PayServlet?opt=status";

    // 签名方式
    private static String sign_type = "RSA2";

    // 字符编码格式
    private static String charset = "utf-8";

    // 支付宝网关
    private static String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";


    /**
     * 支付调用
     *
     * @return
     */
    public static String aliPay(UserPayment userPayment) {
        // 实例化客户端（参数：网关地址、商户appid、商户私钥、格式、编码、支付宝公钥、加密类型），为了取得预付订单信息
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        // 实例化具体API对应的request类
        AlipayTradePagePayRequest payRequest = new AlipayTradePagePayRequest();
        payRequest.setNotifyUrl(AlipayConfig.notify_url);
        payRequest.setReturnUrl(AlipayConfig.return_url);
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式
        AlipayTradePagePayModel payModel = new AlipayTradePagePayModel();
        //商户订单号
        UUID uuid = UUID.randomUUID();
        String uid = uuid.toString();
        payModel.setOutTradeNo(uid);
        //对一笔交易的具体描述信息
        payModel.setBody("智慧管家[" + userPayment.getPaymentname() + "]缴费");
        //商品名称
        payModel.setSubject("业主:" + userPayment.getUsername() + ",缴费:" + userPayment.getValue());
        //交易超时时间 这里的30m就是30分钟
        payModel.setTimeoutExpress("30m");
        //支付金额 后面保留2位小数点..不能超过2位
        payModel.setTotalAmount(String.valueOf(userPayment.getValue()));
        // //电脑网站支付销售产品码，不同的支付方式productCode不同 销售产品码（固定值）
        payModel.setProductCode("FAST_INSTANT_TRADE_PAY");
        payRequest.setBizModel(payModel);
        try {
            // 这里和普通的接口调用不同，使用的是sdkExecute
            //返回支付宝订单信息(预处理)
            AlipayTradePagePayResponse alipayTradePagePayResponse = alipayClient.pageExecute(payRequest);
            //就是orderString 可以直接给APP请求，无需再做处理。
            return alipayTradePagePayResponse.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}